package cn.zcib.film.controller;

import cn.zcib.film.anno.Record;
import cn.zcib.film.bean.Role;
import cn.zcib.film.bean.vo.RoleVO;
import cn.zcib.film.service.AdminService;
import cn.zcib.film.service.RoleService;
import cn.zcib.film.util.DataGridViewResult;
import cn.zcib.film.util.SystemConstants;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private AdminService adminService;

    /**
     * 查询角色列表
     * @return
     */
    @RequestMapping("/list")
    public DataGridViewResult findRoles(RoleVO roleVO){
        //设置分页信息
        PageHelper.startPage(roleVO.getPage(),roleVO.getLimit());
        //调用查询角色方法
        List<Role> roles = roleService.findRoles(roleVO);
        //创建分页对象
        PageInfo<Role> pageInfo = new PageInfo<>(roles);
//        System.out.println("pageInfo = " + pageInfo);
        return new DataGridViewResult(pageInfo.getTotal(),pageInfo.getList());
    }
    @RequestMapping("/getList")
    public List<Role> findRolesNoArgs(){
        return roleService.findRolesNoArgs();
    }

    //添加角色
    @RequestMapping("/add")
    public String addRole(Role role){
        HashMap<String, Object> map = new HashMap<>();

        if (roleService.addRole(role)>0){
            map.put(SystemConstants.SUCCESS, true);
            map.put(SystemConstants.MESSAGE, "添加成功");
        }else {
            map.put(SystemConstants.SUCCESS, false);
            map.put(SystemConstants.MESSAGE, "添加失败");
        }
        return JSON.toJSONString(map);
    }

    //更新角色
    @RequestMapping("/update")
    public String updateRole(Role role){
        HashMap<String, Object> map = new HashMap<>();

        if (roleService.updateRole(role)>0){
            map.put(SystemConstants.SUCCESS, true);
            map.put(SystemConstants.MESSAGE, "修改成功");
        }else {
            map.put(SystemConstants.SUCCESS, false);
            map.put(SystemConstants.MESSAGE, "修改失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 检查角色下是否有用户信息
     * @param rId
     * @return
     */
    @RequestMapping("/checkRoleHasUser")
    public String checkRoleHasUser(Integer rId){
        HashMap<String, Object> map = new HashMap<>();

        if (adminService.getAdminCountByRoleId(rId)>0){
            map.put(SystemConstants.EXIST, true);
            map.put(SystemConstants.MESSAGE, "该角色下存在用户信息，无法删除");
        }else {
            map.put(SystemConstants.EXIST, false);
        }
        return JSON.toJSONString(map);
    }

    /**
     * 删除角色
     * @param rId
     * @return
     */
    @RequestMapping("/deleteById")
    public String deleteById(Integer rId){
        HashMap<String, Object> map = new HashMap<>();

        if (roleService.deleteRole(rId)>0){
            map.put(SystemConstants.SUCCESS, true);
            map.put(SystemConstants.MESSAGE, "删除成功");
        }else {
            map.put(SystemConstants.EXIST, false);
            map.put(SystemConstants.MESSAGE, "删除失败");
        }
        return JSON.toJSONString(map);
    }

    @RequestMapping(value = "/getRole",produces = "application/json;charset = utf-8")
    public String getRole(Integer aId){
        HashMap<String, Object> map = new HashMap<>();
        Role role = roleService.getRoleById(aId);
        try {
            map.put("rName", role.getrName());
            map.put("aName", role.getAdmin().getaName());
        }catch (Exception e){
            System.out.println("e.getCause() = " + e.getCause());
        }
        return JSON.toJSONString(map);
    }
}
